package com.ss.task.test;


import com.ss.task.dto.UserInfo;
import com.ss.task.model.DepartmentEntity;
import com.ss.task.model.LeaderEntity;
import com.ss.task.model.LoginEntity;
import com.ss.task.model.UserEntity;
import com.ss.task.service.UserService;
import com.ss.task.service.TaskService;
import com.ss.task.util.ErrorInfo;
import com.ss.webutil.struct.BaseResult;
import com.ss.webutil.struct.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ss.task.service.UserService;
import com.ss.webutil.struct.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by lifangfang on 2015/10/7.
 */
@RestController
@RequestMapping("/android/")
public class RootController {
    @Autowired
    UserService userService;

    @Autowired
    TaskService taskService;

    @RequestMapping("login")
    public ResultModel<LoginEntity> login(String name, String password, HttpSession session) {
        ResultModel<UserEntity> r = userService.login(name, password);

        LoginEntity le = new LoginEntity();
        if (r.getStatus() != 0) {
            le.setResult(false);
            le.setRealname("null");
        } else {
            UserEntity user = r.getData();
            session.setAttribute("user", user);
            List<LeaderEntity> leaders = userService.getLeaderByUser(user.getId()).getData();
            session.setAttribute("isLeader", leaders.size() != 0);
            session.setAttribute("tasknum", taskService.getCountOfAllPubTasksByMember(user.getId()).getData());
            le.setResult(true);
            le.setRealname(r.getData().getRealname());
        }
        return new ResultModel<LoginEntity>(le);
    }


    @RequestMapping("loginOut")
    public ResultModel<LoginEntity> login(HttpSession session) {
        LoginEntity le = new LoginEntity();
        le.setResult(false);
        le.setRealname("null");
        session.removeAttribute("user");
        session.removeAttribute("isLeader");
        return new ResultModel<LoginEntity>(le);
    }

}
